package com.atguigu.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.product.entity.CategoryEntity;
import com.atguigu.product.service.CategoryService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品三级分类
 *
 * @author polarisink
 * @email polarisink@163.com
 * @date 2020-12-18 14:19:34
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

  @Resource
  private CategoryService categoryService;

  /**
   * 查询所有分类及子分类,以树形解构组装起来
   */
  @RequestMapping("/list/tree")
  public R list() {
    List<CategoryEntity> list = categoryService.listWithTree();
    return R.ok().put("data", list);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{catId}")
  public R info(@PathVariable("catId") Long catId) {
    CategoryEntity category = categoryService.getById(catId);

    return R.ok().put("data", category);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  public R save(@RequestBody CategoryEntity category) {
    categoryService.save(category);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  public R update(@RequestBody CategoryEntity category) {
    categoryService.updateCascade(category);

    return R.ok();
  }

  /**
   * 删除
   *
   * @RequestBody: 获取请求体，必须发post请求
   * SpringMBVC自动将数据(json)转为对象
   */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] catIds) {
    //categoryService.removeByIds(Arrays.asList(catIds));

    categoryService.removeMenuByIds(Arrays.asList(catIds));

    return R.ok();
  }

  @RequestMapping("/update/sort")
  public R updateSort(@RequestBody CategoryEntity[] categoryEntity) {
    categoryService.updateBatchById(Arrays.asList(categoryEntity));
    return R.ok();
  }

}
